<?php
/**
 * @return
 * @author 朱乐斌
 * @date
 */

namespace app\api\controller;

use app\api\model\WallpaperChannelModel;
use app\api\model\WallpaperModel;
use app\api\model\WallpaperTypeModel;
use app\BaseController;
use think\response\Json;

class Wallpaper extends BaseController
{

    /**
     *  获取二级菜单的图片信息
     */
    public function head(): Json
    {
        try {
            // 接收用户点击的数据
            $data = $this->request->get();
            if (empty($data['type_id'])) {
                $data['type_id'] = 1;
            }
            /**
             *  分页查询数据
             *
             */
            // 每页显示多少条
            $pageSize = 25;
            //获取当前页数，默认为第1页
            $page = $data['page'] ?? 1;
            //计算偏移量
            $offset = ($page - 1) * $pageSize;
            $dataLIst = WallpaperModel::where('type_id', $data['type_id'])
                ->where('status', 1)
                ->limit($offset, $pageSize)
                ->field('id,image')
                ->select()->toArray();
            //获取总记录数
            $total = WallpaperModel::where('type_id', $data['type_id'])
                ->where('status', 1)
                ->count();
            //计算总条数
            $totalPage = ceil($total / $pageSize);
//            dump($totalPage);
            if (empty($dataLIst)) {
                return json([
                    'code' => 0,
                    'msg' => '暂无数据',
                    'data' => []
                ]);
            }
            return json([
                'code' => 1,
                'msg' => '成功',
                'data' => ['img' => $dataLIst,
                    'page' => $totalPage]
            ]);
        } catch (\Exception $e) {
            return json($e->getMessage());
        }
    }

    /**
     *  获取一级菜单
     */
    public function topNav(): Json
    {
        try {
            $dataList = WallpaperChannelModel::select();
            if (empty($dataList)) return json(['code' => 0, 'msg' => '获取信息错误']);
            return json([
                'code' => 1,
                'msg' => '成功',
                'data' => $dataList
            ]);
        } catch (\Exception $e) {
            return json($e->getMessage());
        }
    }

    /**
     *  接收一级菜单点击id获取对应的二级菜单
     */
    public function twoList(): Json
    {
        try {
            // 接收用户传递的数据
            $data = $this->request->get();
            if (empty($data)) return json(['code' => 0, 'msg' => '获取信息错误']);
            $dataList = WallpaperTypeModel::where('channel_id', $data['channel_id'])
                ->field('id,name')
                ->select()->toArray();
            if (empty($dataList)) return json(['code' => 0, 'msg' => '没有数据']);
            return json([
                'code' => 1,
                'msg' => '成功',
                'data' => $dataList
            ]);
        } catch (\Exception $e) {
            return json($e->getMessage());
        }
    }

}